<template>
	<xn-form-container
		:title="formData.id ? '编辑账户信息' : '增加账户信息'"
		:width="700"
		v-model:open="open"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="签名类型：" name="signType">
				<a-select v-model:value="formData.signType" placeholder="请选择签名类型" :options="sginOptions" />
			</a-form-item>
			<a-form-item label="商户号：" name="mchId">
				<a-input v-model:value="formData.mchId" placeholder="请输入商户号" allow-clear />
			</a-form-item>
			<a-form-item label="支付的版本号：" name="service">
				<a-input v-model:value="formData.service" placeholder="请输入支付的版本号" allow-clear />
			</a-form-item>
			<a-form-item label="支付账户类型：" name="payType">
				<a-select v-model:value="formData.payType" placeholder="请选择支付账户类型" :options="bodyOptions" />
			</a-form-item>
			<a-form-item label="支付用途：" name="body">
				<a-select v-model:value="formData.body" placeholder="请选择互助金的统一描述" :options="orderOptions" />
			</a-form-item>
			<a-form-item label="请选择账户所属机构：" name="orgId" v-show="formData.body == '支付工会会费'">
				<a-select v-model:value="formData.orgId" placeholder="请选择账户所属机构：" :options="orgOptions" />
			</a-form-item>
			<a-form-item label="秘钥key：" name="wxKey" v-show="formData.payType == '微信账户'">
				<a-input v-model:value="formData.wxKey" placeholder="秘钥key" allow-clear />
			</a-form-item>
			<a-form-item label="微信支付app_id：" name="appId" v-show="formData.payType == '微信账户'">
				<a-input v-model:value="formData.appId" placeholder="微信支付app_id" allow-clear />
			</a-form-item>
			<a-form-item label="微信支付证书存放地址" name="wxCertPath" v-show="formData.id">
				<a-input v-model:value="formData.wxCertPath" placeholder="微信支付证书存放地址" allow-clear disabled />
			</a-form-item>
			<a-form-item label="上传微信支付证书：" name="wxCertPath" v-show="formData.payType == '微信账户' && !formData.id">
				<a-upload
					list-type="picture"
					v-model:file-list="fileList"
					:remove="handleRemove"
					:before-upload="beforeUpload"
					:max-count="1"
					class="upload-list-inline"
				>
					<a-button>
						<upload-outlined></upload-outlined>
						选择证书文件
					</a-button>
				</a-upload>
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="bizPayAccountForm">
import tool from '@/utils/tool'
import { cloneDeep } from 'lodash-es'
import { required } from '@/utils/formRules'
import bizPayAccountApi from '@/api/biz/bizPayAccountApi'
import fileApi from '@/api/biz/bizFileManagerApi.js'

// 抽屉状态
const open = ref(false)
const emit = defineEmits({ successful: null })
const formRef = ref()

const fileList = ref([])

// 表单数据
const formData = ref({})
const submitLoading = ref(false)
const bodyOptions = ref([])
const sginOptions = ref([])
const orderOptions = ref([])
const orgOptions = ref([])

// 打开抽屉
const onOpen = (record) => {
	open.value = true
	if (record) {
		let recordData = cloneDeep(record)
		formData.value = Object.assign({}, recordData)
	}
	bodyOptions.value = tool.dictList('ACCOUNT_TYPE')
	orderOptions.value = tool.dictList('ORDER_TYPE')
	sginOptions.value = tool.dictList('PAY_SGIN_TYPE')
}

// 关闭抽屉
const onClose = () => {
	formRef.value.resetFields()
	formData.value = {}
	open.value = false
	fileList.value = []
}

// 默认要校验的
const formRules = {
	signType: [required('请输入签名类型')],
	mchId: [required('请输入商户号')],
	serivce: [required('请输入支付的版本号')],
	payType: [required('请选择账户类型')]
}

// 文件处理（等选择后手动上传）
const handleRemove = (file) => {
	const index = fileList.value.indexOf(file)
	const newFileList = fileList.value.slice()
	newFileList.splice(index, 1)
	fileList.value = newFileList
	console.log(newFileList)
}

const beforeUpload = (file) => {
	fileList.value = [...fileList.value, file]
	return false
}

// 文件上传
const uploadFile = async () => {
	const formData = new FormData()
	fileList.value.forEach((file) => {
		file = toRaw(file)
		formData.append('files', file.originFileObj)
	})
	return fileApi.fileUploadLocalReturnIdList(formData)
}

// 验证并提交数据
const onSubmit = async () => {
	let res = await uploadFile()
	submitLoading.value = true
	const formDataParam = cloneDeep(formData.value)
	console.log(formDataParam)
	formDataParam.wxCertPath = res[0]
	console.log(formDataParam)
	bizPayAccountApi
		.bizPayAccountSubmitForm(formDataParam, formDataParam.id)
		.then(() => {
			onClose()
			emit('successful')
		})
		.finally(() => {
			submitLoading.value = false
		})
}
// 抛出函数
defineExpose({
	onOpen
})
</script>
